const fileService = require("../service/file.service")
const { updateUserAvatar } = require("../service/user.service")
const {
    SERVER_HOST,
    PORT
} = require('../config/server')

class FileController {
    async create(ctx, next) {
        // 获取对应的信息
        const {
            filename,
            mimetype,
            size
        } = ctx.request.file
        const {
            id
        } = ctx.user
        const result = await fileService.create(filename,
            mimetype,
            size, id)

        const avatarUrl = `${SERVER_HOST}:${PORT}/users/avatar/${id}`
        const result2 = await updateUserAvatar(avatarUrl, id)
        ctx.body = {
            code: 200,
            message: '上传头像成功',
            result,result2
        }
    }
}

module.exports = new FileController()